import QtQuick
import QtQuick.Controls

Window {
    visible: true
    width: 400
    height: 400
    title: "动画测试"

    Rectangle {
        id: box
        width: 50
        height: 50
        color: "red"
        x: 0
        y: parent.height/2 - height/2

        Timer {
            id: moveTimer
            interval: 16 // 约60fps
            repeat: true
            running: true
            onTriggered: {
                box.x += 2
                if (box.x > box.parent.width - box.width) {
                    box.x = 0
                }
            }
        }

        Text {
            anchors.centerIn: parent
            text: Math.round(parent.x)
            color: "white"
        }
    }

    Button {
        text: moveTimer.running ? "暂停动画" : "执行动画"
        onClicked: {
            if (moveTimer.running) {
                moveTimer.stop()
            } else {
                moveTimer.start()
            }
        }
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
}
